<?php

declare(strict_types=1);

namespace App\Listener;

use App\Core\Log;
use App\Event\UserRegister;
use App\Model\PositionSupervisorApply;
use App\Repositories\PositionSupervisorAllowRepository;
use App\Repositories\PositionSupervisorApplyRepository;
use App\Services\AccountService;
use Carbon\Carbon;
use Hyperf\Event\Annotation\Listener;
use Psr\Container\ContainerInterface;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Di\Annotation\Inject;

/**
 * @Listener
 */
class UserRegisterListener implements ListenerInterface
{
    /**
     * @Inject
     * @var AccountService
     */
    private $account_service;
    /**
     * @var ContainerInterface
     */
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function listen(): array
    {
        return [
            UserRegister::class
        ];
    }

    public function process(object $event)
    {
        $user = $event->user;
        $params = $event->params;
        Log::get('api')->info("编号为:".$user->id."用户注册开始",$params);
        $mobile = $user->mobile;
        $allow_flag=true;//是否自动审核通过
//        if($mobile){
//            $allow_exists = PositionSupervisorAllowRepository::getInstance()->one('*',[[
//                'field'=>'mobile',
//                'value' => $mobile
//            ]]);
//            if($allow_exists){
//                $allow_flag = true;
//            }
//        }
        if($allow_flag){
            $apply_result = PositionSupervisorApplyRepository::getInstance()->store([
                'user_id' => $user->id,
                'status' => PositionSupervisorApply::STATUS_SUCCESS
            ]);
            $user->is_supervisor=1;
            $user->be_supervisor_time = date('Y-m-d H:i:s');
            $user->save();
        }else{
            $apply_result = PositionSupervisorApplyRepository::getInstance()->store([
                'user_id' => $user->id,
                'status' => PositionSupervisorApply::STATUS_DEFAULT
            ]);
        }
        Log::get('api')->info("编号为:".$user->id."用户注册结束",$params);
    }
}
